package com.inspectortime.service;

import java.text.DecimalFormat;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.inspectortime.domain.ZipCodeGeoData;
import com.inspectortime.domain.impl.BaseHibernateEntityTestCase;
import com.inspectortime.domain.impl.ZipCodeGeoDataImpl;
import com.inspectortime.repository.Factory;
import com.inspectortime.repository.Repository;

/**
 * Fetches and saves external zip code data from geonames.org. NOT a regular unit test 
 * TODO: Move to a standalone java tool application.
 * 
 */
public class ZipCodeFetcherApp extends BaseHibernateEntityTestCase {

	protected static Log log = LogFactory.getLog(ZipCodeFetcherApp.class);

	public void testFetchAll() throws Exception {
		
		/* 2/27/12: Fails due to spring/hibernate jar upgrade. May want to bring this back at some point.

		Factory factory = (Factory) ctx.getBean("factory");
		Repository repository = (Repository) ctx.getBean("repository");

		ZipCodeSearchService service = new SaxZipCodeSearchServiceImpl();

		DecimalFormat format = new DecimalFormat("00000");
		for (int i = 0; i < 100000; i++) {
			String zip = format.format(i);
			Date start = new Date();
			ZipCodeInfo zipInfo = service.lookupZipCode(zip);
			if (zipInfo != null) {
				ZipCodeGeoData data = repository.findZipCodeGeoData(zipInfo
						.getZipCode());
				if (data == null) {
					data = new ZipCodeGeoDataImpl(zipInfo);
					data.save();
				}
				Date end = new Date();
				long time = end.getTime() - start.getTime();
				log.debug("Read zip " + zip + " in " + time + "ms");
			} else {
				log.debug("Not found: " + zip);
			}
		}
		*/
	}

}
